package weaver.interfaces.zhlx.duss.action.purchasemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-28
 * @Description: 线边仓成本中心领退料流程调用SAP采购非生产采购验收接口(MM043)
 * @ModifyLog:
 **/
public class WeaXbccbzxltllcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        int formid = Math.abs(requestInfo.getRequestManager().getFormid());
        writeLog(">>>>>WeaXbccbzxltllcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            String swdm = Util.null2String(rs.getString("swdm"));
            String pzzdpzrq = Util.null2String(rs.getString("pzzdpzrq"));
            String pzzdgzrq = Util.null2String(rs.getString("pzzdgzrq"));
            String gh = Util.null2String(rs.getString("gh"));
            String cbzx = Util.null2String(rs.getString("cbzx"));
            String ydlxkcgl = Util.null2String(rs.getString("ydlxkcgl"));
            ydlxkcgl = sapUtil.getSelectname(formid, "ydlxkcgl", ydlxkcgl);
            JSONObject dataJson = new JSONObject();
            JSONObject headerJson = new JSONObject();
            headerJson.put("SERIAL", "OA" + lcbh);
            headerJson.put("TCODE", swdm);
            headerJson.put("DOC_DATE", pzzdpzrq.replace("-", ""));
            headerJson.put("PSTNG_DATE", pzzdgzrq.replace("-", ""));
            headerJson.put("HEADER_TXT", lcbh);
            headerJson.put("PR_UNAME", gh);
            JSONArray detailList = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject detailJson = new JSONObject();
                detailJson.put("SERIAL", "OA" + lcbh);
                detailJson.put("SERIALIT", Util.null2String(rs.getString("xxm")));
                detailJson.put("MATERIAL", Util.null2String(rs.getString("ljbh")));
                detailJson.put("BWTAR", Util.null2String(rs.getString("pglx")));
                detailJson.put("PLANT", Util.null2String(rs.getString("gc")));
                detailJson.put("BATCH", Util.null2String(rs.getString("pcbh")));
                detailJson.put("STGE_LOC", Util.null2String(rs.getString("kw")));
                detailJson.put("ENTRY_UOM", Util.null2String(rs.getString("dw")));
                detailJson.put("ENTRY_QNT", Util.null2String(rs.getString("sl")));
                detailJson.put("COSTCENTER", cbzx);
                detailJson.put("MOVE_TYPE", ydlxkcgl);
                detailList.add(detailJson);
            }
            headerJson.put("DETAIL", detailList);
            dataJson.put("HEADER", headerJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaXbccbzxltllcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaXbccbzxltllcAction>>>>>dataJson=" + dataJson);
            if ("201".equals(ydlxkcgl) || "202".equals(ydlxkcgl)) {
                String type = "201".equals(ydlxkcgl) ? "MM035" : "MM043";
                JSONObject resultJson = sapUtil.handleOKHttpRequest(type, dataJson, dataMap);
                writeLog(">>>>>WeaXbccbzxltllcAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    message += outputJson.getString("MSG_TEXT");
                    String dataStr = outputJson.getString("DATA");
                    JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                    JSONArray returnList = JSONArray.parseArray(returnDataJson.getString("RETURN"));
                    if (!"S".equalsIgnoreCase(msgType)) {
                        for (int i = 0; i < returnList.size(); i++) {
                            String msgType2 = returnList.getJSONObject(i).getString("MSG_TYPE");
                            if ("E".equalsIgnoreCase(msgType2)) {
                                String msgText2 = returnList.getJSONObject(i).getString("MSG_TEXT");
                                message += msgText2 + ";";
                            }
                        }
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }else {
                        for (int i = 0; i < returnList.size(); i++) {
                            String msgType2 = returnList.getJSONObject(i).getString("MSG_TYPE");
                            if ("S".equalsIgnoreCase(msgType2)) {
                                String msgText2 = returnList.getJSONObject(i).getString("MSG_TEXT2");
                                String zlfbja = returnList.getJSONObject(i).getString("ZLFBJA");
                                rs.executeUpdate("update "+tablename+" set wlpzbh=?,wlpzbhhjnd=? where requestid=?",msgText2,zlfbja,requestid);
                            }
                        }
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXbccbzxltllcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}